﻿@inject IHttpService _httpService
@inject NavigationManager NavigationManager

<div class="userinfor-card">
    <AuthorizeView>
        <MCard Class="w-100 mb-4 main">
            <MCardText>
                <div>
                    <div class="name">@context.User.Identity.Name</div>
                    <div class="id">@context.User.Claims.GetUserId()</div>
                </div>
                <MButton Fab Dark Color="primary" OnClick="@(()=>NavigationManager.NavigateTo(StaticOptions.IdsUrl+"Account/SelectModifyField"))">
                    <MIcon>mdi-pencil</MIcon>
                </MButton>
            </MCardText>

        </MCard>
    </AuthorizeView>
    <div class="bind mb-4">
        @if (Model == null)
        {
            <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill/>
        }
        else
        {

            <MRow>
                @foreach (var item in Model.AccountFields.Where(s => s.Type != Models.ViewModels.Account.SelectModifyFieldType.Password))
                {
                    <MCol Cols="12" Sm="4" Md="3">
                        <MCard>
                            <MCardText>
                                <div class="key">
                                    <i class="@item.Icon primary--text"></i>
                                    @item.Type.GetDisplayName()
                                </div>
                                @if (item.Actions.Count > 1 || item.Type == Models.ViewModels.Account.SelectModifyFieldType.Email)
                                {
                                    <div class="value success--text">
                                        <i class="mdi mdi-check-circle"></i>
                                        已绑定
                                    </div>
                                }
                                else if (item.Type != Models.ViewModels.Account.SelectModifyFieldType.Password)
                                {
                                    <div class="value warning--text">
                                        <i class="mdi mdi-clock-outline"></i>
                                        未绑定
                                    </div>
                                }
                            </MCardText>
                        </MCard>
                    </MCol>
                }
            </MRow>
            <MRow>
                @foreach (var item in Model.ExternalFields)
                {
                    <MCol Cols="12" Sm="4" Md="3">
                        <MCard>
                            <MCardText>
                                <div class="key">
                                    <i class="@item.Icon primary--text"></i>
                                    @item.DisplayName
                                </div>
                                @if (item.Actions.Count > 1)
                                {
                                    <div class="value success--text">
                                        <i class="mdi mdi-check-circle"></i>
                                        已绑定
                                    </div>
                                }
                                else
                                {
                                    <div class="value warning--text">
                                        <i class="mdi mdi-clock-outline"></i>
                                        未绑定
                                    </div>
                                }
                            </MCardText>


                        </MCard>
                    </MCol>
                }
            </MRow>

        }
    </div>
    <AuthorizeView Context="userContext">
        <MExpansionPanels Focusable Class="mb-4">
            <MExpansionPanel>
                <MExpansionPanelHeader>
                    本地获取到的信息
                </MExpansionPanelHeader>
                <MExpansionPanelContent>
                    <dl class="pt-4">
                        @foreach (var prop in userContext.User.Claims)
                        {
                            <div>
                                <dt>@prop.Type</dt>
                                <dd>@prop.Value</dd>
                            </div>
                        }
                    </dl>
                </MExpansionPanelContent>
            </MExpansionPanel>
        </MExpansionPanels>
    </AuthorizeView>
    @if (Claims != null && Claims.Any())
    {
        <MExpansionPanels Focusable Class="mb-4">
            <MExpansionPanel>
                <MExpansionPanelHeader>
                    API远端获取到的信息
                </MExpansionPanelHeader>
                <MExpansionPanelContent>
                    <dl class="pt-4">
                        @foreach (var prop in Claims)
                        {
                            <div>
                                <dt>@prop.Key</dt>
                                <dd>@prop.Value</dd>
                            </div>
                        }
                    </dl>
                </MExpansionPanelContent>
            </MExpansionPanel>
        </MExpansionPanels>
    }
</div>



@code {
    AccountBindInfor Model;
    List<KeyValuePair<string, string>> Claims;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }



    protected override async Task OnInitializedAsync()
    {
        try
        {
            Model = await _httpService.GetAsync<AccountBindInfor>("api/account/GetBindInfor");
            Claims = await _httpService.GetAsync<List<KeyValuePair<string, string>>>("api/account/GetUserClaims");
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取用户信息失败");
        }
    }

}
